/*
 * Java CMPP API
 * Copyright (C) 2006 by Youming.Xu
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 526 Room, Hong Wa Building,22#,South Section1,
 * YiHuan Road,Sichuan,Chengdu,P.R.China
 * 
 * A copy of the LGPL can be viewed at http://www.gnu.org/copyleft/lesser.html
 * Java CMPP API author: xym008@gmail.net
 * Java CMPP API Homepage: http://cmppapi.sourceforge.net/
 * SubmitSMResp.java,v 1.10 2006-7-59:49:25 xuym Exp
 */

package com.bci.cmpp.message;

import java.io.UnsupportedEncodingException;

import com.bci.cmpp.util.CMPPIO;
import com.bci.cmpp.util.InvalidDateFormatException;

/**
 * SubmitSMResp.java
 * 
 * @author xuym
 * @version 1.0
 * 
 */
public class CMPPSubmitResp30 extends CMPPSubmitResp {

	public CMPPSubmitResp30() {
		super();
		this.totalLength = 24;
		this.buffer = new byte[this.totalLength];
	}

	/**
	 * with the exception of command_id and total_length
	 * 
	 * @throws UnsupportedEncodingException
	 * @throws InvalidDateFormatException
	 */
	public CMPPSubmitResp30(byte[] buf) throws InvalidParameterValueException,
			InvalidDateFormatException, UnsupportedEncodingException {
		this();
		if (buf == null || buf.length < this.totalLength)
			throw new InvalidParameterValueException("input bytes is invlid.",
					"");
		this.readFrom(buf, 0);
	}

	public byte[] getBytes() {
		// if (this.buffer == null || this.buffer.length != this.totalLength) {
		this.buffer = new byte[this.totalLength];
		CMPPIO.intToBytes(this.totalLength, 4, this.buffer, 0);
		CMPPIO.intToBytes(this.commandId, 4, this.buffer, 4);
		CMPPIO.intToBytes(this.sequenceNum, 4, this.buffer, 8);
		CMPPIO.longToBytes(this.msgId, 8, this.buffer, 12);
		CMPPIO.intToBytes(this.commandStatus, 4, this.buffer, 20);
		// }
		return this.buffer;
	}

	public void readFrom(byte[] buf, int offset)
			throws InvalidParameterValueException {
		if (buf == null || buf.length < this.totalLength + offset)
			throw new InvalidParameterValueException("input bytes is invlid.",
					"");
		/** get the sequence number */
		this.sequenceNum = CMPPIO.bytesToInt(buf, 8 + offset, 4);
		/** get the message id */
		this.msgId = CMPPIO.bytesToLong(buf, 12 + offset, 8);
		/** get the command status */
		this.commandStatus = CMPPIO.bytesToInt(buf, 20 + offset, 4);
		System.arraycopy(buf, offset, buffer, 0, this.totalLength);

	}

}
